<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserLogic
 *
 * @author Administrator
 */
class UserLogic extends BaseLogic{
    //put your code here
    private $userTable;
    public function __construct() {
        parent::__construct();
        $this->userTable=  $this->load("user");
        $this->table=  $this->userTable;
    }
    /**
     * 判断用户是否存在，存在返回true，否则返回false
     * @param type $name 字符串名称
     * @param type $type 类型：#1为手机号码，#2为邮件名称，#3为用户名称
     * @return boolean 存在返回true，不存在返回false
     */
    public function existUser($name,$type=1){
        $count=0;
        switch ($type){
            case 1:
               $count=  $this->userTable->get_count("phone='{$name}'");
                break;
            case 2:
                $count=  $this->userTable->get_count("email='{$name}'");
                break;
            case 3:
                $count=  $this->userTable->get_count("base_name='{$name}'");
                break;
        }
        if($count>0){
            return TRUE;
        }else{
            return FALSE;
        }
    }
    /**
     * 获得第三方用户信息
     * @param type $third_uid 第三方appi
     * @return boolean 返回用户信息 否则返回false
     */
    public function getThirdUser($third_uid){
        if($third_uid){
            $user=  $this->userTable->get_one("id,password,is_third,third_uid,image,create_time,ip,login_time,status,login_type,city,province,area,address,gender,nickname,lng,lat,email,base_name,phone", "is_third=1 and third_uid='{$third_uid}' and status=1");
            return $user;
        }
        return FALSE;
    }
    /**
     * 获得用户信息
     * @param type $user_id 用户id
     * @return boolean  用户信息
     */
    public function getUserInfo($user_id) {
       if($user_id){
           $user=  $this->userTable->get_one("id,password,is_third,third_uid,image,create_time,ip,login_time,status,login_type,city,province,area,address,gender,nickname,lng,lat,city_id,area_id,email,base_name,phone", "id={$user_id} and status=1");
           return $user;
       }
       return FALSE;
    }

    /**
     * 第三方登录的用户是否存在
     * @param type $third_uid 第三方用户appid
     * @return boolean 存在返回true 
     */
    public function existThirdUser($third_uid){
        $count=0;
        if($third_uid){
            $count=  $this->userTable->get_count("is_third=1 and third_uid='{$third_uid}' and status=1");
            if($count>0){
                return TRUE;
            }
        }
        return FALSE;
    }
    /**
     * 获得用户所在地区的天气,设定好时间
     * @param type $user_id  用户id
     * @param type $year     年份
     * @param type $month    月份
     * @param type $day      日期
     * @return boolean       成功返回天气信息否则返回false
     */
    public function getUserWeather($user_id,$year,$month,$day){
        if($user_id>0){
            $cityLogic=$this->uselogic("City");
            $areaLogic=$this->uselogic("Area");
            $weatherLogic=  $this->uselogic("Weather");
            $user= $this->getUserInfo($user_id);
             $weatherID="";
             if(isset($user["city_id"])){
                $city=$cityLogic->getCityInfoById($user["city_id"]);
                if(isset($city["weatherID"])&&$city["weatherID"]!=""){
                    $weatherID=$city["weatherID"];
                }
            }
            if(isset($user["area_id"])){
                $area=$areaLogic->getAreaInfoById($user["area_id"]);
                if(isset($area["weatherID"])&&$area["weatherID"]!=""){
                    $weatherID=$area["weatherID"];
                }
            }
            
              if($weatherID!=""){
                $date_time=  strtotime("{$year}-{$month}-{$day} 00:00:00");
                $weather=$weatherLogic->getWeatherByWeatherID($weatherID,$date_time);
                if($weather){
                    return $weather;
                }
              }
            
        }
        return FALSE;
    }
    
    
    /**
     * 更新地址信息
     * @param type $id 用户id
     * @param type $province  省份
     * @param type $city  城市
     * @param type $area  区域
     * @param type $address 定位地址
     */
    public function updateLocation($id=0,$province="",$city="",$area="",$address="",$lng=0.0,$lat=0.0){
        $user_data=array();
            if($city!=""){
                $city_id=  $this->uselogic("City")->getIdByName($city);
                $user_data["city"]=$city;
                $user_data["city_id"]=$city_id;
            }
            if($province!=""){
                $province_id=  $this->uselogic("Province")->getIdByName($province);
                $user_data["province"]=$province;
                $user_data["province_id"]=$province_id;
            }
            if($area!=""){
                $area_id=  $this->uselogic("area")->getIdByName($area);
                $user_data["area_id"]=$area_id;
                $user_data["area"]=$area;
            }
            if($address!=""){
                $user_data["address"]=$address;
            }
            if($lng!=0){
                $user_data["lng"]=$lng;
            }
            if($lat!=0){
                $user_data["lat"]=$lat;
            }
            if($id>0&&!empty($user_data)){
                $this->save($user_data,$id);
            }
    }

    /**
     *    保存和更新用户列表
     * @param type $data 用户信息
     * @param type $id   用户id
     * @return type  
     */
    public function save($data,$id=0){
        if($id>0){
            $res=  $this->userTable->update($data,"id={$id}");
        }else{
            $res=$this->userTable->insert($data,TRUE);
        }
        return $res;
    }
    /**
     *   用户登录
     * @param type $name  用户名，可以是手机 邮箱 用户名
     * @param type $password  用户密码
     * @param type $type 登录类型#103用户名称登录,#104用户手机号码登录,#105电子邮箱登录
     * @return boolean  成功返回用户信息，否则返回false
     */
    public function login($name,$password,$type=104){
        $where="";
        $data="id,password,is_third,third_uid,image,create_time,ip,login_time,status,login_type,city,province,area,address,gender,nickname,lng,lat,email,base_name,phone";
        switch ($type){
            case 104:
                $where="phone='{$name}'";
                $data.=",phone as uname";
                break;
            case 103:
                $where="base_name='{$name}'";
                $data.=",base_name as uname";
                break;
            case 105:
                $where="email='{$name}'";
                $data.=",email as uname";
                break;
            default :
                $where="1=1";
                break;
        }
        $res=  $this->userTable->get_one($data,$where." and password='{$password}'");
        if($res){
            $data=array(
                "login_time"=>  time(),
                "ip"=>  Common::getIp(),
                "login_type"=>$type
            );
            $this->save($data,$res["id"]);
            return $res;
        }
        return FALSE;
    }
}
